<template>
  <list-card>
    <template v-slot:search>
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="公告标题" prop="subject">
          <el-input v-model="queryParams.subject" clearable placeholder="请输入公告标题"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="公告类型">
            <el-option v-for="item in localDict.notice_type" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSearch && !calender" label="发布时间">
          <el-date-picker v-model="queryParams.params.loginTime" :default-time="['00:00:00', '23:59:59']"
                          type="daterange"
                          start-placeholder="开始日期" range-separator="-" end-placeholder="结束日期"
                          value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:operate>
      <el-button v-has-permi="['msg:notice:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['msg:notice:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList">
        <template v-slot:append>
          <el-tooltip effect="dark" :content="calender ? '日历显示' : '表格显示'" placement="top">
            <el-button size="mini" circle :icon="calender ? 'el-icon-date' : 'el-icon-s-order'"
                       @click="calender = !calender"/>
          </el-tooltip>
        </template>
      </right-toolbar>
    </template>
    <template v-slot:content>
      <div v-if="calender">
        <el-calendar>
          <template
            slot="dateCell"
            slot-scope="{data}">
            <p :class="data.isSelected ? 'is-selected' : ''">
              {{ data.day}} {{ data.isSelected ? '✔️' : '' }}
            </p>
          </template>
        </el-calendar>
      </div>
      <div v-else>
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column align="center" type="selection" width="55"/>
          <el-table-column align="center" label="序号" prop="noticeId" width="100"/>
          <el-table-column label="公告标题" prop="noticeTitle" show-overflow-tooltip/>
          <el-table-column align="center" label="公告类型" prop="noticeType" width="100">
            <template v-slot:default="scope">
              <dict-tag :options="localDict.notice_type" :value="scope.row.noticeType"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态" prop="status" width="100">
            <template v-slot:default="scope">
              <dict-tag :options="localDict.notice_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建者" prop="createBy" width="100"/>
          <el-table-column align="center" label="创建时间" prop="createTime" width="100"/>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template v-slot:default="scope">
              <el-button v-has-permi="['msg:notice:edit']" icon="el-icon-edit" size="mini" type="text"
                         @click="handleEdit(scope.row)">修改
              </el-button>
              <el-button v-has-permi="['system:notice:remove']" icon="el-icon-delete" size="mini" type="text"
                         @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="pageParam.total>0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                    :page.sync="pageParam.pageNum" @pagination="getList"/>
      </div>
    </template>
  </list-card>
</template>

<script>

import {list, remove} from "../../../api/base";
import listMixin from "../../../mixins/listMixin";

export default {
  name: "MsgCalendar",
  components: {},
  mixins: {listMixin},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      baseUrl: '/msg/notice',
      calender: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      // 公告表格数据
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 弹出层标题
      queryParams: {
        params: {}
      },
      localDict: {
        notice_type: [],
        notice_status: [],
      },
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    getLocalDict() {

    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
        this.dataList = response.rows;
        this.pageParam.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id || this.ids
      this.$modal.confirm('是否确认删除选择的站内通知？').then(() => {
        remove(this.baseUrl, noticeIds).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    }
  }
};
</script>
